package com.key.win.system.dao;

import com.key.win.base.mapper.KeyWinMapper;
import com.key.win.system.model.SysOrgan;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SysOrganDao extends KeyWinMapper<SysOrgan> {

    @Select(" select o.* from sys_organ o where (select count(1) from sys_organ e where e.parent_id=o.id) = 0 and o.enable_flag = 1")
    List<SysOrgan> findLeafNode();

    @Select("SELECT o.* FROM ( SELECT @pId AS pId, ( SELECT @pId := GROUP_CONCAT(id) FROM sys_organ WHERE FIND_IN_SET(parent_Id, @pId)) AS p FROM sys_organ, (SELECT @pId := #{parentId}) b WHERE @pId IS NOT NULL ) temp, sys_organ o WHERE FIND_IN_SET(o.parent_Id, temp.pId) AND o.enable_flag = 1")
    List<SysOrgan> findChildrenNode(String parentId);


    @Select(" SELECT o.* FROM ( SELECT @pId AS pId, ( SELECT @pId := parent_id FROM sys_organ WHERE FIND_IN_SET(id, @pId)) AS p FROM sys_organ, ( SELECT @pId := #{id} ) b WHERE @pId IS NOT NULL ) temp, sys_organ o WHERE FIND_IN_SET(o.id, temp.pId) AND o.enable_flag = 1 ")
    List<SysOrgan> findParentNode(String id);
}
